<!-- user表单弹窗 -->
<form id="card-form" lay-filter="card-form"
    class="layui-form model-form">
    <input  name="type" type="hidden" value="3"/>
    <div class="layui-form-item">
        <label class="layui-form-label">顾客</label>
        <div class="layui-input-block">
            <select lay-filter="customerName" name="customerId" lay-verify="required" lay-search>
                <option value="">请选择顾客</option> 
                    <% for(customer in customers) { %>
                        <option value="${customer.id}">${customer.customerName}</option>
                    <% } %>
            </select>
        </div>
    </div>
    
    <!-- <div class="layui-form-item">
        <label class="layui-form-label">购买数量</label>
        <div class="layui-input-block">
            <input  name="amount" placeholder="请输入数量" type="text"
                class="layui-input" />
        </div>
    </div> -->
    
    
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">验证码</label>
            <div class="layui-input-block">
                <input  name="verificationCode" placeholder="请输入验证码" type="text"
                class="layui-input" />
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" id="smsSend">发送验证码</button>
            </div>
        </div>
    </div>
    
    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary" type="button"
            ew-event="closeDialog">取消</button>
        <button class="layui-btn" lay-filter="form-submit" lay-submit>购买</button>
    </div>
</form>


<script>
    layui.use([ 'layer', 'admin', 'form', 'formSelects', 'upload' ],
                    function() {
                        var $ = layui.jquery;
                        var layer = layui.layer;
                        var admin = layui.admin;
                        var form = layui.form;
                        var upload = layui.upload;
                        var formSelects = layui.formSelects;

                        form.render('radio');
                        form.render('select');

                        var url = 'service/productSale/add';
                        // 回显user数据
                        var order =
                        // 表单提交事件
                        form.on('submit(form-submit)', function(data) {
                            layer.load(2);
                            data.field.shopId = $("#shopInfo").attr("data");
                            data.field.orderDetail = ${orderList};
                          admin.req(url, data.field, function(data) {
                                layer.closeAll('loading');
                                if (data.code == 1) {
                                    layer.msg( data.msg,{
                                                icon : 1,
                                                time : 1000
                                            },
                                            function() {
                                                admin.finishPopupCenter();
                                            })
                                    
                                } else {
                                    layer.msg(data.msg, {
                                        icon : 2
                                    });
                                }
                            }, "post");
                            return false;
                        });
                        
                        var customerId = null;
                        form.on('select(customerName)', function(data){
                            customerId = data.value;
                            $("#customerId").val(data.value);
                        });      
                        
                        
                        $("#smsSend").click(function () {
                            var url = 'service/productSale/sms';
                            if(customerId == null){ 
                                layer.msg("请先选择顾客信息。");  
                                return false; 
                            } 
                            admin.req(url,{customerId: customerId}, function(data) {
                                layer.closeAll('loading');
                                if (data.code == 1) {
                                    layer.msg('获取验证码成功');
                                    time();
                                } else {
                                    layer.msg('获取验证码失败');
                                }
                            }, "post");
                            return false;
                        })
                        
                    });
    
                    var wait=60;
                    function time(){
                        
                        if (wait==0) {
                            $("#smsSend").attr("disabled",false); 
                            $("#smsSend").html("获取验证码");
                            $("#smsSend").css("background","#fe9900");
                            wait=60;
                        }else{
                            $("#smsSend").attr("disabled", true);
                            $("#smsSend").html(wait+"秒后重新获取");
                            $("#smsSend").css("background","#8f8b8b");
                            wait--;
                            setTimeout(function(){
                                this.time();
                            },1000)
                        }
                    }

</script>